<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="名称
品牌名称
" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称
品牌名称
"></el-input>
    </el-form-item>
    <el-form-item label="上级标识" prop="pid">
      <el-input v-model="dataForm.pid" placeholder="上级标识"></el-input>
    </el-form-item>
    <el-form-item label="Price价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="Price价格"></el-input>
    </el-form-item>
    <el-form-item label="series系列" prop="series">
      <el-input v-model="dataForm.series" placeholder="series系列"></el-input>
    </el-form-item>
    <el-form-item label="型号type" prop="type">
      <el-input v-model="dataForm.type" placeholder="型号type"></el-input>
    </el-form-item>
    <el-form-item label="CPU型号cputype" prop="cputype">
      <el-input v-model="dataForm.cputype" placeholder="CPU型号cputype"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          pid: '',
          price: '',
          series: '',
          type: '',
          cputype: ''
        },
        dataRule: {
          name: [
            { required: true, message: '名称
品牌名称
不能为空', trigger: 'blur' }
          ],
          pid: [
            { required: true, message: '上级标识不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: 'Price价格不能为空', trigger: 'blur' }
          ],
          series: [
            { required: true, message: 'series系列不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '型号type不能为空', trigger: 'blur' }
          ],
          cputype: [
            { required: true, message: 'CPU型号cputype不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/kernel/mobile/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.mobile.name
                this.dataForm.pid = data.mobile.pid
                this.dataForm.price = data.mobile.price
                this.dataForm.series = data.mobile.series
                this.dataForm.type = data.mobile.type
                this.dataForm.cputype = data.mobile.cputype
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/kernel/mobile/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'pid': this.dataForm.pid,
                'price': this.dataForm.price,
                'series': this.dataForm.series,
                'type': this.dataForm.type,
                'cputype': this.dataForm.cputype
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
